/*
Copyright (c) 2007 Emmanuel Bonnet, blog.emmanuelbonnet.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package wiiless
{
	
	import fl.transitions.Tween;
	import fl.transitions.easing.Strong;
	import flash.events.EventDispatcher;
	import wiiless.objects.ObjectsList;
	import wiiless.wiiless.WiilessList;
	import wiiless.wiiless.Wiiless;
	import wiiless.events.WiilessEvent;
	import flash.media.Video;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import wiiless.core.ACursor;
	import wiiless.objects.AObjectMove;
	import flash.display.BitmapData;
		
	public class WiilessManager extends EventDispatcher
	{
		
		/**
		 * Si initialisé
		 */
		private var _isInit:Boolean = false;
		
		/**
		 * Liste des objets
		 */
		private var _objectsList:ObjectsList;
		
		/**
		 * Liste des wiiless
		 */
		private var _wiilessList:WiilessList;
		
		/**
		 * Vidéo
		 */
		private var _video:Video;
		
		/**
		 * Bitmap Source
		 */
		private var _bmp:BitmapData;
		
		/**
		 * Container
		 */
		private var _container:Sprite;
		
		/**
		 * Temps du tween
		 */
		private var _iTimeTween:int = 8;
		
		/**
		 * Si voir les curseurs des wiiless
		 */
		private var _bViewCursor:Boolean;
		
		/**
		 * Constructeur
		 * 
		 * @param 	pContainer		Container des wiiless
		 */
		function WiilessManager(pContainer:Sprite)
		{
			_container = pContainer;
			_objectsList = new ObjectsList();
			_wiilessList = new WiilessList();
		}
		
		/**
		 * Ajout d'une wiiless
		 * 
		 * @param	pWiiless		Wiiless
		 */
		public function addWiiless(pWiiless:Wiiless):void
		{			
			var wl:Wiiless = pWiiless;
			
			wl.addEventListener(WiilessEvent.CONNECT, _onConnect);
			wl.addEventListener(WiilessEvent.DISCONNECT, _onDisconnect);
			wl.addEventListener(WiilessEvent.PRESS, _onPress);
			wl.addEventListener(WiilessEvent.RELEASE, _onRelease);
			wl.addEventListener(WiilessEvent.WHEEL, _onWheel);
			wl.addEventListener(WiilessEvent.MOVE, _onMove);
			
			_wiilessList.addWiiless(wl);
			
			wl.connect(_container, _bmp ? _bmp : null);
			
		}
		
		/**
		 * Connexion d'une wiiless
		 * 
		 * @param	e		WiilessEvent
		 */
		private function _onConnect(e:WiilessEvent):void
		{
			if(!_isInit)
			{
				var firstWl:Wiiless = _wiilessList.getWiilessById(e.id);
				_bmp = firstWl.bmp
				_video = firstWl.video;
				_isInit = true;
			}
			var ev:WiilessEvent = new WiilessEvent(WiilessEvent.CONNECT);
			ev.id = e.id;
			dispatchEvent(ev);
		}
		
		/**
		 * Déconnexion d'une wiiless
		 */
		private function _onDisconnect(e:WiilessEvent):void
		{

		}
		
		/**
		 * Déplacement d'une wiiless
		 */
		private function _onMove(e:WiilessEvent):void
		{
			
			var wl:Wiiless = _wiilessList.getWiilessById(e.id);
			
			if(wl.cursor)
			{
				
				if(_bViewCursor)
				{
					if(e.point)
					{
						wl.cursor.visible = true;
						var tX:Tween = new Tween(wl.cursor, "x", Strong.easeOut, wl.cursor.x, e.point.x, _iTimeTween);
						var tY:Tween = new Tween(wl.cursor, "y", Strong.easeOut, wl.cursor.y, e.point.y, _iTimeTween);
					}
					else {
						wl.cursor.visible = false;
					}
				}
				
				var aObj:Array = _objectsList.getList();
				var nObjects:int = aObj.length;
				var i:int = -1;
				while(++i < nObjects)
				{
					var obj:AObjectMove = aObj[i];
					if(obj)
					{
						if(e.id == obj.wiiless || obj.wiiless == 0)
						{
							var n:int;
							if(e.point)
							{
								if(obj.hitTestPoint(_container.x+e.point.x, _container.y+e.point.y))
								{
									n = obj.addWiilessHit(wl);

									if(n>0)
										obj.over();
									
									var tClipX:Tween;
									var tClipY:Tween;
									if(n==1 && obj.autoMove)
									{										
										obj.move();
										
										tClipX = new Tween(obj, "x", Strong.easeOut, obj.x, e.point.x, _iTimeTween);
										tClipY = new Tween(obj, "y", Strong.easeOut, obj.y, e.point.y, _iTimeTween);
										if(obj.scaleMode == AObjectMove.SCALE_DEPTH)
										{
											obj.scaleDepth();
											
											var tClipSX:Tween = new Tween(obj, "z", Strong.easeOut, obj.z, e.point.z, _iTimeTween);
											var tClipSY:Tween = new Tween(obj, "z", Strong.easeOut, obj.z, e.point.z, _iTimeTween);
										}
									}
									else if(n == 2 && obj.scaleMode == AObjectMove.SCALE_DOUBLE) {
									
										obj.scaleDouble();
										
										var wFirst:Wiiless = obj.getWiilessHit()[0];
										var wSecond:Wiiless = obj.getWiilessHit()[1];
										
										var nDx:Number = Math.abs(obj.x-wSecond.currentPoint.x);
										var nDy:Number = Math.abs(obj.y-wSecond.currentPoint.y);
										
										var p:Object = wFirst.currentPoint;
										
										tClipX = new Tween(obj, "x", Strong.easeOut, obj.x, p.x, _iTimeTween);
										tClipY = new Tween(obj, "y", Strong.easeOut, obj.y, p.y, _iTimeTween);
										
										var iWidth:int = Math.floor(Math.abs(wFirst.currentPoint.x-wSecond.currentPoint.x))*2+50;
										var iHeight:int = Math.floor(Math.abs(wFirst.currentPoint.y-wSecond.currentPoint.y))*2+50;
										if(obj.scaleProp)
										{
											var nScale:Number = nDx>nDy ? iWidth/obj.width : iHeight/obj.height;
											iWidth = obj.width*nScale;
											iHeight = obj.height*nScale;
										}
										
										iWidth = iWidth>obj.minWidth ? iWidth : obj.minWidth;
										iHeight = iHeight>obj.minHeight ? iHeight : obj.minHeight;
										
										var tW:Tween = new Tween(obj, "width", Strong.easeOut, obj.width, iWidth, _iTimeTween);
										var tH:Tween = new Tween(obj, "height", Strong.easeOut, obj.height, iHeight, _iTimeTween);
									}
								}
								else {
									n = obj.removeWiilessHit(wl);
								}
							}
							else {
								n = obj.removeWiilessHit(wl);
							}
						if(!n>0)
							obj.out();
						}
					}
				}
			}
		}
		
		/**
		 * Pression sur le stage
		 */
		private function _onPress(e:WiilessEvent):void
		{
			
		}
		
		/**
		 * Relachement sur le stage
		 */
		private function _onRelease(e:WiilessEvent):void
		{

		}
		
		/**
		 * 
		 */
		private function _onWheel(e:WiilessEvent):void
		{

		}
		
		/**
		 * Ajout d'un objet
		 * 
		 * @param	pChild		AObjectMove à ajouter
		 * @param	pWilless	Wiiless à attribué à l'objet
		 */
		public function addObject(pChild:AObjectMove, pWiiless:int=0):void
		{
			pChild.wiiless = pWiiless;
			_objectsList.addObject(pChild);
		}
		
		/**
		 * Suppression d'un objet
		 * 
		 * @param	pChild		AObjectMove à supprimer
		 */
		public function removeObject(pChild:AObjectMove):void
		{
			_objectsList.removeObject(pChild);
		}
		
		/**
		 * Suppression d'un objet par son id
		 * 
		 * @param	pId		Id de l'AObjectMove à supprimer
		 */
		public function removeObjectById(pId:int):void
		{
			_objectsList.removeObjectById(pId);
		}
		
		/**
		 * Renvoi la liste des objets
		 */
		public function getObjectsList():ObjectsList
		{
			return _objectsList;
		}
		
		// --------------------------- get/setter
		
		/**
		 * Vidéo
		 */
		public function get video():Video
		{
			return _video;
		}
		
		/**
		 * Voir les curseurs
		 */
		public function get viewCursor():Boolean
		{
			return _bViewCursor;
		}
		
		public function set viewCursor(pB:Boolean):void
		{
			_bViewCursor = pB;
		}
		
	}
}